import subprocess
# import os
# os.system("pip3 install IPY")
# from IPY import IP #导入IPY模块
ip = input("请输入要检查的网段：")
# ip = IP(ip) #直接查询ip网段内所有的ip地址
# for i in ip:
#     retcode = subprocess.call(["ping", "-c,"1",i])
#     if not retcode:
#         print(f"{i} 已存在！")
#     else:
#         print(f"{i} 不存在！")
# retcode = subprocess.call(["ping","-n","1",ip]) #cmd中ping
def IP(ip): #去除IP网段最后一位
    ip = ip[::-1]
    d4 = ip.index('.')
    ip = ip[::-1]
    ip = ip[0:len(ip) - d4]
    return ip

def allIP(ip):    #生成网段所有IP函数
    iplist = []
    for i in range(1,255):
        ip = IP(ip) + str(i)
        iplist[i] = ip
    return iplist

def check_ip(ip): #检查IP是否存在函数
    retcode = subprocess.call(["ping","-c","1",ip]) #通过call()执行系统命令
    if not retcode:
        return 1
    else:
        return 0
IPlist = allIP(ip)
for i in IPlist:
    print(check_ip(i) and f"{i}已存在！" or f"{i}不存在！")